// 实现这个项目的构建任务
const sass = require('sass');
const loadGruntTasks = require('load-grunt-tasks');

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

module.exports = grunt => {
  grunt.initConfig({
    clean: {
      //所要清除的文件路径
      files: 'dist/**'
    },
    sass: {
      options: {
        sourceMap: true,
        implementation: sass
      },
      main: {
        files: {
          // 目标文件：源文件
          'dist/assets/styles/main.css': 'src/assets/styles/main.scss'
        }
      }
    },
    babel: {
      options: {
        sourceMap: true,
        presets: ['@babel/preset-env']
      },
      main: {
        files: {
          // 目标文件：源文件
          'dist/assets/scripts/index.js': 'src/assets/scripts/main.js'
        }
      }
    },
    // imagemin: { // gifsicle 安装失败。。。
    //   image: {
    //     expand: true,
    //     cwd: 'src/',
    //     src: 'assets/images/**',
    //     dest: 'dist/',
    //   },
    //   font: {
    //     expand: true,
    //     cwd: 'src/',
    //     src: 'assets/font/**',
    //     dest: 'dist/',
    //   },
    // },
    copy: {
      main: {
        expand: true,
        cwd: 'public',
        src: ['**'],
        dest: 'dist/public',
      },
      image: {
        expand: true,
        cwd: 'src/',
        src: 'assets/images/**',
        dest: 'dist/',
      },
      font: {
        expand: true,
        cwd: 'src/',
        src: 'assets/fonts/**',
        dest: 'dist/',
      }
    },
    web_swig: {
      options: {
        swigOptions: {
          cache: false
        },
        getData: function (tpl) {
          //模板文件的数据
          return data;
        }
      },
      main: {
        expand: true,
        cwd: 'src/', // 源文件文件夹
        src: '**/*.html', // 后缀名匹配
        dest: 'dist/' // 目标文件夹
      },
    },
    watch: {
      js: {
        files: ['src/assets/scripts/*.js'],
        tasks: ['babel']
      },
      css: {
        files: ['src/assets/styles/*.scss'],
        tasks: ['sass']
      },
      html: {
        files: ['src/*.html'],
        tasks: ['web_swig']
      }
    },
    browserSync: {
      bsFiles: {
        src: [
          'dist/css/*.css',
          'dist/*.html'
        ]
      },
      options: {
        watchTask: true,
        server: {
          baseDir: ['dist', 'src', 'public'],
          routes: {
            '/node_modules': 'node_modules'
          }
        }
      }
    },
  })

  // grunt.loadNpmTasks('grunt-sass');
  loadGruntTasks(grunt); // 自动加载所有grunt插件中的任务
  grunt.registerTask('build', ['sass', 'babel', 'web_swig', 'copy']);
  grunt.registerTask('start', ['sass', 'babel', 'web_swig', 'copy', 'browserSync', 'watch']);
}